/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.test;

import com.comac.ins.common.json.utils.JsonUtils;
import com.comac.ins.isfd.constant.enums.IsfdAlgorithmConfigLanguageEnum;
import com.comac.ins.isfd.constant.enums.IsfdAlgorithmConfigTypeEnum;
import com.comac.ins.isfd.domain.bo.IsfdAlgorithmConfigBo;
import com.comac.ins.isfd.domain.vo.IsfdAlgorithmConfigVo;
import com.comac.ins.isfd.service.IIsfdAlgorithmConfigService;
import com.comac.ins.isfd.util.IsfdAlgorithmConfigPythonExecute;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class IsfdAlgorithmConfigServiceTest {

    @Autowired
    private IIsfdAlgorithmConfigService isfdAlgorithmConfigService;

    @Autowired
    private IsfdAlgorithmConfigPythonExecute isfdAlgorithmConfigPythonExecute;

    @Test
    void test() {
        IsfdAlgorithmConfigVo isfdAlgorithmConfigVo = isfdAlgorithmConfigService.queryByOneByName("calculate_l_profile");
        Map<String, Object> data = new HashMap<>();
        data.put("layer_parameters", Arrays.asList(Arrays.asList(0.005, 200000, 80000)));
        data.put("shape_parameters", Arrays.asList(0.2, 0.1));
        String jsonData = JsonUtils.toJsonString(data);
        Map result = isfdAlgorithmConfigPythonExecute.excute(isfdAlgorithmConfigVo.getAlgorithmName(), isfdAlgorithmConfigVo.getImplementation(), jsonData);
        System.out.println(result);


        IsfdAlgorithmConfigVo isfdAlgorithmConfigVo1 = isfdAlgorithmConfigService.queryByOneByName("calculate_fz_profile");
        Map<String, Object> data1 = new HashMap<>();
        data1.put("layer_parameters", Arrays.asList(Arrays.asList(2.244, 47897.9133790099, 28470.2773615631)));
        data1.put("shape_parameters", Arrays.asList(15, 80, 28, 5));
        String jsonData1 = JsonUtils.toJsonString(data1);
        Map result1 = isfdAlgorithmConfigPythonExecute.excute(isfdAlgorithmConfigVo1.getAlgorithmName(), isfdAlgorithmConfigVo1.getImplementation(), jsonData1);
        System.out.println(result1);
    }


    @Test
    public void test2() {
        IsfdAlgorithmConfigBo isfdAlgorithmConfigBo = new IsfdAlgorithmConfigBo();
        isfdAlgorithmConfigBo.setId(1921844772857565666L);
        String implementation = """
            def compute_i_beam_properties(params):
                layer_parameters = params['layer_parameters']
                shape_parameters = params['shape_parameters']
                T1 = layer_parameters[0][0]
                E1 = layer_parameters[0][1]
                G1 = layer_parameters[0][2]
                rho1 = layer_parameters[0][3]
                THM1 = layer_parameters[0][4]

                T2 = layer_parameters[1][0]
                E2 = layer_parameters[1][1]
                G2 = layer_parameters[1][2]
                RHO2 = layer_parameters[1][3]
                THM2 = layer_parameters[1][4]

                T3 = layer_parameters[2][0]
                E3 = layer_parameters[2][1]
                G3 = layer_parameters[2][2]
                RHO3 = layer_parameters[2][3]
                THM3 = layer_parameters[2][4]

                W1 = shape_parameters[0]
                H = shape_parameters[1]
                W2 = shape_parameters[2]

                # 计算各部分面积
                web_area = T2 * (H - T1 - T3)
                bottom_flange_area = T1 * W1
                top_flange_area = T3 * W2
               \s
                # 总面积
                total_area = bottom_flange_area + web_area + top_flange_area
               \s
                # 计算等效材料属性
                equivalent_E = (E1*bottom_flange_area + E2*web_area + E3*top_flange_area) / total_area
                equivalent_G = (G1*bottom_flange_area + G2*web_area + G3*top_flange_area) / total_area
                equivalent_rho = (rho1*bottom_flange_area + RHO2*web_area + RHO3*top_flange_area) / total_area
                equivalent_thm = (E1*bottom_flange_area*THM1 + E2*web_area*THM2 + E3*top_flange_area*THM3) / (equivalent_E*total_area)
               \s
                # 计算形心位置
                centroid = (E2*web_area*(H-T3+T1)/2 + E1*bottom_flange_area*(T1/2) + E3*top_flange_area*(H-T3/2)) / (total_area*equivalent_E)
               \s
                # 计算主惯性矩 I_xx（修复语法错误）
                I_xx = (bottom_flange_area * pow(centroid - T1/2, 2)) * E1/equivalent_E
                I_xx += (web_area * pow(H/2 + T1/2 - T3/2 - centroid, 2)) * E2/equivalent_E
                I_xx += (T2 * pow(H - T1 - T3, 3) / 12) * E2/equivalent_E
                I_xx += (top_flange_area * pow(H - centroid - T3/2, 2)) * E3/equivalent_E
               \s
                # 计算次惯性矩 I_yy
                I_yy = T1 * pow(W1, 3) / 12 * E1/equivalent_E + T3 * pow(W2, 3) / 12 * E3/equivalent_E

                return {
                    "E_E": equivalent_E,
                    "E_G": equivalent_G,
                    "XC": centroid,
                    "A_ALL": total_area,
                    "I_ALL": I_xx,
                    "I_ADD": I_yy,
                    "E_RHO": equivalent_rho,
                    "E_THM": equivalent_thm
                }
                 """;
        isfdAlgorithmConfigBo.setImplementation(implementation);
        isfdAlgorithmConfigBo.setAlgorithmName("compute_i_beam_properties");
        isfdAlgorithmConfigBo.setAlgorithmType(IsfdAlgorithmConfigTypeEnum.SECTION.getValue());
        isfdAlgorithmConfigBo.setAlgorithmLanguage(IsfdAlgorithmConfigLanguageEnum.PYTHON.getValue());
        isfdAlgorithmConfigBo.setDescription("计算I型梁截面属性(改进版)-包括弹性模量、剪切模量、形心位置、面积、惯性矩、密度和热膨胀系数");
        isfdAlgorithmConfigService.updateByBo(isfdAlgorithmConfigBo);

//        IsfdAlgorithmConfigBo isfdAlgorithmConfigBo1 = new IsfdAlgorithmConfigBo();
//        String implementation1 = """
//            def calculate_fc_profile(params):
//                layer_parameters = params['layer_parameters']
//                shape_parameters = params['shape_parameters']
//                T1 = layer_parameters[0][0]
//                E1 = layer_parameters[0][1]
//                G1 = layer_parameters[0][2]
//                rho1 = layer_parameters[0][3]
//                THM1 = layer_parameters[0][4]
//
//                T2 = layer_parameters[1][0]
//                E2 = layer_parameters[1][1]
//                G2 = layer_parameters[1][2]
//                RHO2 = layer_parameters[1][3]
//                THM2 = layer_parameters[1][4]
//
//                T3 = layer_parameters[2][0]
//                E3 = layer_parameters[2][1]
//                G3 = layer_parameters[2][2]
//                RHO3 = layer_parameters[2][3]
//                THM3 = layer_parameters[2][4]
//
//                W1 = shape_parameters[0]
//                H = shape_parameters[1]
//                W2 = shape_parameters[2]
//                SD = shape_parameters[3]
//
//                # 竖边面积
//                ST2 = T2 * (H - T1 - T3)
//                # 底边面积
//                ST1 = T1 * W1
//                # 顶边面积
//                ST3 = T3 * W2
//                # 总面积
//                A_ALL = ST1 + ST2 + ST3
//                # 等效模量
//                E_E = (E1 * ST1 + E2 * ST2 + E3 * ST3) / A_ALL
//                E_G = (G1 * ST1 + G2 * ST2 + G3 * ST3) / A_ALL
//                # 等效密度
//                E_RHO = (rho1 * ST1 + RHO2 * ST2 + RHO3 * ST3) / A_ALL
//                # 等效热膨胀系数
//                E_THM = (E1 * ST1 * THM1 + E2 * ST2 * THM2 + E3 * ST3 * THM3) / (E_E * A_ALL)
//                # 型心
//                XC = (E1 * ST1 * (T1) / 2 + E2 * ST2 * (H - T3 + T1) / 2 + E3 * ST3 * (H - T3 / 2)) / A_ALL / E_E
//                YC = (E1 * ST1 * W1 / 2 + E2 * ST2 * T2 / 2 + E3 * ST3 * W2 / 2) / A_ALL / E_E
//                # 主惯性矩（型心水平轴）
//                I_ALL = (ST1 * pow(XC - T1 / 2, 2)) * E1 / E_E
//                I_ALL += (ST2 * pow(H / 2 + T1 / 2 - T3 / 2 - XC, 2)) * E2 / E_E
//                I_ALL += (T2 * pow(H - T1 - T3, 3) / 12) * E2 / E_E
//                I_ALL += (ST3 * pow(H - XC - T3 / 2, 2)) * E3 / E_E
//                # 惯性矩（型心垂直轴）
//                I_ADD = (T1 * pow(W1, 3) / 12 + ST1 * pow(W1 - YC, 2)) * E1 / E_E
//                I_ADD += ST2 * pow(YC - T2 / 2, 2) * E2 / E_E
//                I_ADD += (T3 * pow(W2, 3) / 12 + ST3 * pow(W2 - YC, 2)) * E3 / E_E
//
//                return {
//                    "E_E": E_E,
//                    "E_G": E_G,
//                    "XC": XC,
//                    "A_ALL": A_ALL,
//                    "I_ALL": I_ALL,
//                    "I_ADD": I_ADD,
//                    "E_RHO": E_RHO,
//                    "E_THM": E_THM
//                }
//               """;
//        isfdAlgorithmConfigBo1.setImplementation(implementation1);
//        isfdAlgorithmConfigBo1.setAlgorithmName("calculate_fc_profile");
//        isfdAlgorithmConfigBo1.setAlgorithmType(IsfdAlgorithmConfigTypeEnum.SECTION.getValue());
//        isfdAlgorithmConfigBo1.setAlgorithmLanguage(IsfdAlgorithmConfigLanguageEnum.PYTHON.getValue());
//        isfdAlgorithmConfigBo1.setDescription("型材默认算法-计算C型金属框属性");
//        isfdAlgorithmConfigService.insertByBo(isfdAlgorithmConfigBo1);
//
//
//        IsfdAlgorithmConfigBo isfdAlgorithmConfigBo2 = new IsfdAlgorithmConfigBo();
//        String implementation2 = """
//            def calculate_fg_profile(params):
//                layer_parameters = params['layer_parameters']
//                shape_parameters = params['shape_parameters']
//                T1 = layer_parameters[0][0]
//                E1 = layer_parameters[0][1]
//                G1 = layer_parameters[0][2]
//
//                W = shape_parameters[0]
//
//                # 竖边面积
//                ST2 = T1 * W
//                # 总面积
//                A_ALL = ST2
//                # 等效模量
//                E_E = E1
//                E_G = G1
//                # 型心
//                XC = T1 / 2
//                # 惯性矩
//                I_ALL = W * pow(T1, 3) / 12
//                I_ADD = T1 * pow(W, 3) / 12
//
//                return {
//                    "E_E": E_E,
//                    "E_G": E_G,
//                    "XC": XC,
//                    "A_ALL": A_ALL,
//                    "I_ALL": I_ALL,
//                    "I_ADD": I_ADD
//                }
//                 """;
//        isfdAlgorithmConfigBo2.setImplementation(implementation2);
//        isfdAlgorithmConfigBo2.setAlgorithmName("calculate_fg_profile");
//        isfdAlgorithmConfigBo2.setAlgorithmType(IsfdAlgorithmConfigTypeEnum.SECTION.getValue());
//        isfdAlgorithmConfigBo2.setAlgorithmLanguage(IsfdAlgorithmConfigLanguageEnum.PYTHON.getValue());
//        isfdAlgorithmConfigBo2.setDescription("型材默认算法-计算'一'缘条属性");
//        isfdAlgorithmConfigService.insertByBo(isfdAlgorithmConfigBo2);
//
//
//        IsfdAlgorithmConfigBo isfdAlgorithmConfigBo3 = new IsfdAlgorithmConfigBo();
//        String implementation3 = """
//            import math
//
//
//            def calculate_fz_profile(params):
//                layer_parameters = params['layer_parameters']
//                shape_parameters = params['shape_parameters']
//                T1 = layer_parameters[0][0]
//                E1 = layer_parameters[0][1]
//                G1 = layer_parameters[0][2]
//
//                W1 = shape_parameters[0]
//                H = shape_parameters[1]
//                W2 = shape_parameters[2]
//                R = shape_parameters[3]
//
//                # 圆角面积
//                SR1 = math.pi * (-pow(R, 2) + pow(R + T1, 2)) / 4
//                # 竖边面积
//                ST2 = T1 * (H - 2 * (R + T1))
//                # 底边面积
//                ST1 = T1 * (W1 - (R + T1))
//                # 顶边面积
//                ST3 = T1 * (W2 - (T1 + R))
//                # 总面积
//                A_ALL = ST1 + ST2 + ST3 + 2 * SR1
//                # 等效模量
//                E_E = E1
//                E_G = G1
//                # 圆环形心位置
//                XRC = 4 * (-pow(R, 3) + pow(R + T1, 3)) / 3 / math.pi / (-pow(R, 2) + pow(R + T1, 2))
//                # 型心
//                XC = (ST1 * T1 / 2 + ST3 * (H - T1 / 2) + ST2 * H / 2 + SR1 * H) / A_ALL
//                YC = (ST1 * (W1 + W2 - T1 - (W1 - T1 - R) / 2) + ST3 * (W2 - T1 - R) / 2 + ST2 * (W2 - T1 / 2) + SR1 * (
//                            W2 * 2 - T1)) / A_ALL
//                # 主惯性矩（型心水平轴）
//                I_ALL = (ST1 * pow(XC - T1 / 2, 2)) + (ST2 * pow(H / 2 - XC, 2)) + (T1 * pow(H - 2 * (R + T1), 3) / 12)
//                I_ALL += (ST3 * pow(H - XC - T1 / 2, 2)) + 2 * SR1 * pow(H / 2 - XC, 2) + 2 * SR1 * pow(H / 2 - (R + T1) + XRC, 2)
//                # 惯性矩（型心垂直轴）
//                I_ADD = (T1 * pow(W1 - T1 - R, 3) / 12 + ST1 * pow(W1 + W2 - T1 - (W1 - T1 - R) / 2 - YC, 2))
//                I_ADD += ST2 * pow(W2 - T1 / 2 - YC, 2) + (T1 * pow(W2 - T1 - R, 3) / 12 + ST3 * pow(W2 - T1 - R - YC, 2))
//                I_ADD += SR1 * pow(W2 - T1 - XRC - YC, 2) + SR1 * pow(W2 + XRC - YC, 2)
//
//                return {
//                    "E_E": E_E,
//                    "E_G": E_G,
//                    "XC": XC,
//                    "A_ALL": A_ALL,
//                    "I_ALL": I_ALL,
//                    "I_ADD": I_ADD
//                }
//                """;
//        isfdAlgorithmConfigBo3.setImplementation(implementation3);
//        isfdAlgorithmConfigBo3.setAlgorithmName("calculate_fz_profile");
//        isfdAlgorithmConfigBo3.setAlgorithmType(IsfdAlgorithmConfigTypeEnum.SECTION.getValue());
//        isfdAlgorithmConfigBo3.setAlgorithmLanguage(IsfdAlgorithmConfigLanguageEnum.PYTHON.getValue());
//        isfdAlgorithmConfigBo3.setDescription("型材默认算法-计算Z型框属性");
//        isfdAlgorithmConfigService.insertByBo(isfdAlgorithmConfigBo3);
//
//
//        IsfdAlgorithmConfigBo isfdAlgorithmConfigBo4 = new IsfdAlgorithmConfigBo();
//        String implementation4 = """
//            def calculate_i_profile(params):
//                layer_parameters = params['layer_parameters']
//                shape_parameters = params['shape_parameters']
//                T1 = layer_parameters[0][0]
//                E1 = layer_parameters[0][1]
//                G1 = layer_parameters[0][2]
//                rho1 = layer_parameters[0][3]
//                THM1 = layer_parameters[0][4]
//
//                T2 = layer_parameters[1][0]
//                E2 = layer_parameters[1][1]
//                G2 = layer_parameters[1][2]
//                RHO2 = layer_parameters[1][3]
//                THM2 = layer_parameters[1][4]
//
//                T3 = layer_parameters[2][0]
//                E3 = layer_parameters[2][1]
//                G3 = layer_parameters[2][2]
//                RHO3 = layer_parameters[2][3]
//                THM3 = layer_parameters[2][4]
//
//                W1 = shape_parameters[0]
//                H = shape_parameters[1]
//                W2 = shape_parameters[2]
//
//                # 竖边面积
//                ST2 = T2 * (H - T1 - T3)
//                # 底边面积
//                ST1 = T1 * W1
//                # 顶边面积
//                ST3 = T3 * W2
//                # 总面积
//                A_ALL = ST1 + ST2 + ST3
//                # 等效模量
//                E_E = (E1 * ST1 + E2 * ST2 + E3 * ST3) / A_ALL
//                E_G = (G1 * ST1 + G2 * ST2 + G3 * ST3) / A_ALL
//                # 等效密度
//                E_RHO = (rho1 * ST1 + RHO2 * ST2 + RHO3 * ST3) / A_ALL
//                # 等效热膨胀系数
//                E_THM = (E1 * ST1 * THM1 + E2 * ST2 * THM2 + E3 * ST3 * THM3) / (E_E * A_ALL)
//                # 型心
//                XC = (E2 * ST2 * (H - T3 + T1) / 2 + E1 * ST1 * (T1) / 2 + E3 * ST3 * (H - T3 / 2)) / A_ALL / E_E
//                # 惯性矩（型心水平轴）
//                I_ALL = (ST1 * pow(XC - T1 / 2, 2)) * E1 / E_E  # 底边惯性矩
//                I_ALL += (ST2 * pow(H / 2 + T1 / 2 - T3 / 2 - XC, 2)) * E2 / E_E  # 竖边惯性矩平行轴移动
//                I_ALL += (T2 * pow(H - T1 - T3, 3) / 12) * E2 / E_E  # 竖边惯性矩
//                I_ALL += (ST3 * pow(H - XC - T3 / 2, 2)) * E3 / E_E  # 顶边惯性矩
//                # 惯性矩（型心垂直轴）
//                I_ADD = T1 * pow(W1, 3) / 12 * E1 / E_E + T3 * pow(W2, 3) / 12 * E3 / E_E
//
//                return {
//                    "E_E": E_E,
//                    "E_G": E_G,
//                    "XC": XC,
//                    "A_ALL": A_ALL,
//                    "I_ALL": I_ALL,
//                    "I_ADD": I_ADD,
//                    "E_RHO": E_RHO,
//                    "E_THM": E_THM
//                }
//                 """;
//        isfdAlgorithmConfigBo4.setImplementation(implementation4);
//        isfdAlgorithmConfigBo4.setAlgorithmName("calculate_i_profile");
//        isfdAlgorithmConfigBo4.setAlgorithmType(IsfdAlgorithmConfigTypeEnum.SECTION.getValue());
//        isfdAlgorithmConfigBo4.setAlgorithmLanguage(IsfdAlgorithmConfigLanguageEnum.PYTHON.getValue());
//        isfdAlgorithmConfigBo4.setDescription("型材默认算法-计算I型型材属性");
//        isfdAlgorithmConfigService.insertByBo(isfdAlgorithmConfigBo4);
//
//
//        IsfdAlgorithmConfigBo isfdAlgorithmConfigBo5 = new IsfdAlgorithmConfigBo();
//        String implementation5 = """
//            def calculate_t_profile(params):
//                layer_parameters = params['layer_parameters']
//                shape_parameters = params['shape_parameters']
//                T1 = layer_parameters[0][0]
//                E1 = layer_parameters[0][1]
//                G1 = layer_parameters[0][2]
//                rho1 = layer_parameters[0][3]
//                THM1 = layer_parameters[0][4]
//
//                T2 = layer_parameters[1][0]
//                E2 = layer_parameters[1][1]
//                G2 = layer_parameters[1][2]
//                RHO2 = layer_parameters[1][3]
//                THM2 = layer_parameters[1][4]
//
//                W = shape_parameters[0]
//                H = shape_parameters[1]
//
//                # 竖边面积
//                ST1 = T1 * (H - T2)
//                # 横边面积
//                ST2 = T2 * W
//                # 总面积
//                A_ALL = ST1 + ST2
//                # 等效模量
//                E_E = (E1 * ST1 + E2 * ST2) / A_ALL
//                E_G = (G1 * ST1 + G2 * ST2) / A_ALL
//                # 等效密度
//                E_RHO = (rho1 * ST1 + RHO2 * ST2) / A_ALL
//                # 等效热膨胀系数
//                E_THM = (E1 * ST1 * THM1 + E2 * ST2 * THM2) / (E_E * A_ALL)
//                # 型心
//                XC = (E1 * ST1 * (H + T2) / 2 + E2 * ST2 * (T2) / 2) / A_ALL / E_E
//                # 主惯性矩（型心水平轴）
//                I_ALL = (ST2 * pow(XC - T2 / 2, 2)) * E2 / E_E
//                I_ALL += (ST1 * pow(H / 2 + T2 / 2 - XC, 2)) * E1 / E_E
//                I_ALL += (T1 * pow(H - T2, 3) / 12) * E1 / E_E
//                # 惯性矩（型心垂直轴）
//                I_ADD = T2 * pow(W, 3) / 12 * E2 / E_E + (H - T2) * pow(T1, 3) / 12 * E1 / E_E
//
//                return {
//                    "E_E": E_E,
//                    "E_G": E_G,
//                    "XC": XC,
//                    "A_ALL": A_ALL,
//                    "I_ALL": I_ALL,
//                    "I_ADD": I_ADD,
//                    "E_RHO": E_RHO,
//                    "E_THM": E_THM
//                }
//                             """;
//        isfdAlgorithmConfigBo5.setImplementation(implementation5);
//        isfdAlgorithmConfigBo5.setAlgorithmName("calculate_t_profile");
//        isfdAlgorithmConfigBo5.setAlgorithmType(IsfdAlgorithmConfigTypeEnum.SECTION.getValue());
//        isfdAlgorithmConfigBo5.setAlgorithmLanguage(IsfdAlgorithmConfigLanguageEnum.PYTHON.getValue());
//        isfdAlgorithmConfigBo5.setDescription("型材默认算法-计算T型长桁属性");
//        isfdAlgorithmConfigService.insertByBo(isfdAlgorithmConfigBo5);
    }
}
